<?php

namespace App\Admin\Controllers\Agents;

use App\Admin\Repositories\ShortUrl;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ShortUrlController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new ShortUrl(), function (Grid $grid) {
            $grid->model()->orderBy('created_at','desc');
            $grid->column('id')->sortable();
            $grid->column('agent_id')->filter();
            $grid->column('link')->copyable()->qrcode(function () {
                return $this->link;
            }, 200, 200);
            $grid->column('out_video')->limit(60);
            $grid->column('url')->limit(60);
            // $grid->column('key');
            // $grid->column('password');
            $grid->column('state')->using([1 => '使用中', 0 => '已停用',-1=>'被封禁'])->label([
                'default' => 'primary', // 设置默认颜色，不设置则默认为 default
                0 => 'info',
                -1 => 'danger',
                1 => 'success',
            ]);
            $grid->column('created_at')->sortable();
        
            $grid->disableCreateButton();
            
            
            $grid->disableFilterButton();
            $grid->quickSearch(['id', 'url', 'link']);
            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('link');
        
            });
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableView();
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new ShortUrl(), function (Show $show) {
            $show->field('id');
            $show->field('agent_id');
            $show->field('link');
            $show->field('url');
            $show->field('key');
            $show->field('password');
            $show->field('state');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ShortUrl(), function (Form $form) {
            $form->display('id');
            $form->display('agent_id');
            $form->text('link')->required();
            $form->text('url');
            // $form->text('key');
            $form->text('out_video');
            // $form->text('password');
            $form->radio('state')->options([1 => '启用', 0=> '停用',-1=>'封禁'])->default(1);
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
